//
//  LCZHistoryViewController.m
//  PersonalNews
//
//  Created by Golibyo on 15/12/2.
//  Copyright © 2015年 changshuhua. All rights reserved.
//

#import "LCZHistoryViewController.h"
#import "LCZCalendarTool.h"
#import "LCZcalendarCell.h"
@interface LCZHistoryViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)LCZBaseCollectionView *collectionView;
@property(nonatomic, strong)NSDate *date;
@property(nonatomic, strong)LCZBaseTableView *tableView;
@property(nonatomic, strong)NSMutableArray *tableViewArr;
@property(nonatomic, strong)NSMutableArray *array;
@property(nonatomic, assign)NSInteger lastMonthTotleDays;

@property(nonatomic, strong)NSDate *lastMonthDate;
@property(nonatomic, strong)NSDate *nextMonthDate;
@property(nonatomic, strong)NSArray *weekDaysArr;
//显示label
@property(nonatomic, strong)LCZBaseLabel *presentTimeLabel;
@property(nonatomic, strong)UICollectionViewFlowLayout *flowlayout;
@property(nonatomic, assign)BOOL setCollectionViewTypeButtonIsClick;
//三个数组.分别对应当前月份 last月份 next月份
@property(nonatomic, strong)NSMutableArray *presentDayArr;
@property(nonatomic, strong)NSMutableArray *lastDayArr;
@property(nonatomic, strong)NSMutableArray *nextDayArr;
@property(nonatomic, strong)UIButton *setCollectionViewTypeButton;
@property(nonatomic, strong)LCZBaseLabel *bottomLineLabel;
@end




@implementation LCZHistoryViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.weekDaysArr = @[@"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六"];
        self.date = [NSDate date];
        [self createPresentDayArr];
        [self createLastDayArr];
        [self createNextDayArr];
    }
    return self;
}

//创建上个月份数组
- (void)createLastDayArr {
    self.lastDayArr = [NSMutableArray array];
    for (NSInteger i = 1; i <= [LCZCalendarTool firstWeekdayInThisMonth:self.date]; i++) {
        [self.lastDayArr addObject:[NSString stringWithFormat:@"%ld", [LCZCalendarTool totaldaysInMonth:[LCZCalendarTool lastMonth:self.date]] - [LCZCalendarTool firstWeekdayInThisMonth:self.date] + i]];
    }

}

//创建当前月份数组
- (void)createPresentDayArr {
    self.presentDayArr = [NSMutableArray array];
    for (NSInteger i = 1; i <= [LCZCalendarTool totaldaysInMonth:self.date]; i++) {
        [self.presentDayArr addObject:[NSString stringWithFormat:@"%ld", i]];
    }

}

//创建下个月份数组
- (void)createNextDayArr {
    self.nextDayArr = [NSMutableArray array];
    for (NSInteger i = 0; i <= 42 - [LCZCalendarTool totaldaysInMonth:self.date] - [LCZCalendarTool firstWeekdayInThisMonth:self.date]; i++) {
        [self.nextDayArr addObject:[NSString stringWithFormat:@"%ld", i]];
    }

}



- (void)createBackButton {
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(15, 30, 30, 30);
    backButton.layer.cornerRadius = 15;
    
    backButton.backgroundColor = [UIColor colorWithRed:0.45 green:0.46 blue:0.46 alpha:1];
    [self.view addSubview:backButton];
    [backButton setImage:[UIImage imageNamed:@"left.png"] forState:UIControlStateNormal];
    [backButton setTintColor:[UIColor whiteColor]];
    [backButton addTarget:self action:@selector(popBack:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)createColletionView {
    self.flowlayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowlayout.itemSize = CGSizeMake(WIDTH / 375.0 * 50, HEIGHT / 667.0 * 40);
    self.flowlayout.minimumInteritemSpacing = WIDTH / 375.0 * 3;
    self.flowlayout.minimumLineSpacing = HEIGHT / 667.0 * 4;
    
    self.collectionView = [[LCZBaseCollectionView alloc] initWithFrame:CGRectMake(0, HEIGHT / 667.0 * 150, WIDTH, HEIGHT / 667.0 * 260) collectionViewLayout:self.flowlayout];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:1 green:0.81 blue:0.86 alpha:1];
    [self.collectionView registerClass:[LCZcalendarCell class] forCellWithReuseIdentifier:@"reuse"];
    self.bottomLineLabel = [[LCZBaseLabel alloc] initWithFrame:CGRectMake(0, HEIGHT / 667.0 * 425, WIDTH, HEIGHT / 667.0 * 1)];
    self.bottomLineLabel.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.bottomLineLabel];
}

- (void)createWeekdaylabel {
    for (NSInteger i = 0; i < 7; i++) {
        LCZBaseLabel *label = [[LCZBaseLabel alloc] initWithFrame:CGRectMake(WIDTH / 7.0 * i, HEIGHT / 667.0 * 120, WIDTH / 7.0, HEIGHT / 667.0 * 30)];
        [self.view addSubview:label];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:WIDTH / 375.0 * 12];
        label.text = self.weekDaysArr[i];
        
    }
}

- (void)createTitleSet {
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    f.dateFormat = @"yyyy-MM";
    //      date -> string
    NSString *dateStr = [f stringFromDate:self.date];
    self.presentTimeLabel = [[LCZBaseLabel alloc] initWithFrame:CGRectMake(WIDTH / 8.0 * 3, HEIGHT / 667.0 * 90, WIDTH / 8.0 * 2, HEIGHT / 667.0 * 30)];
    self.presentTimeLabel.backgroundColor = [UIColor grayColor];
    self.presentTimeLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:self.presentTimeLabel];
    self.presentTimeLabel.textAlignment = NSTextAlignmentCenter;
    self.presentTimeLabel.text = dateStr;
    UIButton *lastMonthButton = [UIButton buttonWithType:UIButtonTypeSystem];
    lastMonthButton.frame = CGRectMake(WIDTH / 8.0 * 2, HEIGHT / 667.0 * 90, WIDTH / 375.0 * 30, WIDTH / 375.0 * 30);
    lastMonthButton.layer.cornerRadius = WIDTH / 375.0 * 15;
    lastMonthButton.tintColor = [UIColor whiteColor];
    lastMonthButton.backgroundColor = [UIColor grayColor];
    [self.view addSubview:lastMonthButton];
    [lastMonthButton setImage:[UIImage imageNamed:@"lefts.png"] forState:UIControlStateNormal];
    UIButton *nextMonthButton = [UIButton buttonWithType:UIButtonTypeSystem];
    nextMonthButton.frame = CGRectMake(WIDTH / 8.0 * 6 - WIDTH / 375.0 * 30, HEIGHT / 667.0 * 90, WIDTH / 375.0 * 30, WIDTH / 375.0 * 30);
    nextMonthButton.layer.cornerRadius = WIDTH / 375.0 * 15;
    nextMonthButton.tintColor = [UIColor whiteColor];
    [self.view addSubview:nextMonthButton];
    [nextMonthButton setImage:[UIImage imageNamed:@"right.png"] forState:UIControlStateNormal];
    nextMonthButton.backgroundColor = [UIColor grayColor];
    [lastMonthButton addTarget:self action:@selector(changeCalendar:) forControlEvents:UIControlEventTouchUpInside];
    lastMonthButton.tag = 1000;
    [nextMonthButton addTarget:self action:@selector(changeCalendar:) forControlEvents:UIControlEventTouchUpInside];
    self.setCollectionViewTypeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.setCollectionViewTypeButton.frame = CGRectMake(WIDTH / 375.0 * 320, HEIGHT / 667.0 * 410, WIDTH / 375.0 * 30, WIDTH / 375.0 * 30);
    self.setCollectionViewTypeButton.backgroundColor = [UIColor colorWithRed:1 green:0.81 blue:0.86 alpha:1];
    self.setCollectionViewTypeButton.layer.cornerRadius = WIDTH / 375.0 * 15;
    [self.setCollectionViewTypeButton setImage:[UIImage imageNamed:@"up.png"] forState:UIControlStateNormal];
    self.setCollectionViewTypeButton.tintColor = [UIColor whiteColor];
    self.setCollectionViewTypeButton.layer.borderWidth = WIDTH / 375.0 * 1;
    self.setCollectionViewTypeButton.layer.borderColor = [[UIColor grayColor] CGColor];
    [self.setCollectionViewTypeButton addTarget:self action:@selector(changeCollectionViewType:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.setCollectionViewTypeButton];
}

- (void)changeCollectionViewType:(UIButton *)button {
//    self.presentMonthParameter = 0;
//    self.lastMonthParameter = 0;
//    self.nextmonthparameter = 0;
    if (self.setCollectionViewTypeButtonIsClick) {
        self.collectionView.frame = CGRectMake(0, HEIGHT / 667.0 * 150, WIDTH, HEIGHT / 667.0 * 260);
        self.flowlayout.scrollDirection = 0;
        self.flowlayout.minimumLineSpacing = HEIGHT / 667.0 * 4;
        self.setCollectionViewTypeButtonIsClick = NO;
        self.setCollectionViewTypeButton.frame = CGRectMake(WIDTH / 375.0 * 320, HEIGHT / 667.0 * 410, WIDTH / 375.0 * 30, HEIGHT / 667.0 * 30);
        self.bottomLineLabel.frame = CGRectMake(0, HEIGHT / 667.0 * 425, WIDTH, HEIGHT / 667.0 * 1);
        [self.setCollectionViewTypeButton setImage:[UIImage imageNamed:@"up.png"] forState:UIControlStateNormal];
        self.tableView.frame = CGRectMake(0, HEIGHT / 667.0 * 445, WIDTH, HEIGHT / 667.0 * 232);
    } else {
        self.tableView.frame = CGRectMake(0, HEIGHT / 667.0 * 245, WIDTH, HEIGHT / 667.0 * 432);
        self.collectionView.frame = CGRectMake(0, HEIGHT / 667.0 * 150, WIDTH, HEIGHT / 667.0 * 40);
        self.flowlayout.scrollDirection = 1;
        self.flowlayout.minimumLineSpacing = 0;
        self.setCollectionViewTypeButtonIsClick = YES;
        self.setCollectionViewTypeButton.frame = CGRectMake(WIDTH / 375.0 * 320, HEIGHT / 667.0 * 190, WIDTH / 375.0 * 30, HEIGHT / 667.0 * 30);
        self.bottomLineLabel.frame = CGRectMake(0, HEIGHT / 667.0 * 205, WIDTH, HEIGHT / 667.0 * 1);
        [self.setCollectionViewTypeButton setImage:[UIImage imageNamed:@"down.png"] forState:UIControlStateNormal];
    }
}

- (void)changeCalendar:(UIButton *)button {
    if (button.tag == 1000) {
        self.date = [LCZCalendarTool lastMonth:self.date];
        
        [self createPresentDayArr];
        [self createLastDayArr];
        [self createNextDayArr];
    } else {
        self.date = [LCZCalendarTool nextMonth:self.date];
        [self createPresentDayArr];
        [self createLastDayArr];
        [self createNextDayArr];
    }
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    f.dateFormat = @"yyyy-MM";
    //      date -> string
    NSString *dateStr = [f stringFromDate:self.date];
    self.presentTimeLabel.text = dateStr;
//    self.presentMonthParameter = 0;
//    self.lastMonthParameter = 0;
//    self.nextmonthparameter = 0;
    [self.collectionView reloadData];
}

- (void)createTableView {
    self.tableView = [[LCZBaseTableView alloc] initWithFrame:CGRectMake(0, HEIGHT / 667.0 * 445, WIDTH, HEIGHT / 667.0 * 232) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[LCZBaseTableViewCell class] forCellReuseIdentifier:@"treuse"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableViewArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LCZBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"treuse" forIndexPath:indexPath];
    cell.textLabel.text = self.tableViewArr[indexPath.row][@"myTitle"];
    return cell;
}

- (void)viewDidLoad {
    self.array = [NSMutableArray array];
    
    self.array = [LCZCacheSaveTool cacheSaveGet:@"Calendar/calendar.plist"];
    NSLog(@"%@", [LCZCacheSaveTool cacheSaveGet:@"Calendar/calendar.plist"]);
    
    self.view.backgroundColor = [UIColor colorWithRed:1 green:0.81 blue:0.86 alpha:1];
    
    [self createBackButton];
    [self createColletionView];
    [self createWeekdaylabel];
    [self createTitleSet];
    [self createTableView];
    [LCZCalendarTool day:self.date];
    [LCZCalendarTool month:self.date];
    [LCZCalendarTool year:self.date];
    [LCZCalendarTool firstWeekdayInThisMonth:self.date];
//    上个月份的date
    self.lastMonthDate = [LCZCalendarTool lastMonth:self.date];
//    下个月份的date
    self.nextMonthDate = [LCZCalendarTool nextMonth:self.date];
    [LCZCalendarTool totaldaysInMonth:self.date];
      self.lastMonthTotleDays = [LCZCalendarTool month:[LCZCalendarTool lastMonth:self.date]];
    
    //    NSLog(@"%ld-%ld-%ld", [LCZCalendarTool day:self.date], [LCZCalendarTool month:self.date], [LCZCalendarTool year:self.date]);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 42;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCZcalendarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    if (indexPath.row < [LCZCalendarTool firstWeekdayInThisMonth:self.date]) {
        cell.label.backgroundColor = [UIColor grayColor];
//        cell.label.text = [NSString stringWithFormat:@"%ld", [LCZCalendarTool totaldaysInMonth:self.lastMonthDate] - [LCZCalendarTool firstWeekdayInThisMonth:self.date] + self.lastMonthParameter + 1];
//        有问题
        cell.label.text = self.lastDayArr[indexPath.row];
//        self.lastMonthParameter ++;
        
    } else if (indexPath.row > ([LCZCalendarTool totaldaysInMonth:self.date] + [LCZCalendarTool firstWeekdayInThisMonth:self.date] - 1)) {
        cell.label.backgroundColor = [UIColor grayColor];
//        self.nextmonthparameter ++;
//        cell.label.text = [NSString stringWithFormat:@"%ld", self.nextmonthparameter];
        cell.label.text = self.nextDayArr[1 + indexPath.row - [LCZCalendarTool firstWeekdayInThisMonth:self.date] - [LCZCalendarTool totaldaysInMonth:self.date]];
    }
         else {
//        self.presentMonthParameter ++;
//        cell.label.text = [NSString stringWithFormat:@"%ld",self.presentMonthParameter];
             cell.label.text = self.presentDayArr[indexPath.row - [LCZCalendarTool firstWeekdayInThisMonth:self.date]];
             cell.label.backgroundColor = [UIColor blackColor];
             
    }

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LCZcalendarCell *cell = (LCZcalendarCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    NSLog(@"%@", cell.label.text);
    NSString *str;
    NSString *monthStr = [self.presentTimeLabel.text substringFromIndex:5];
    self.tableViewArr = [NSMutableArray array];
    if (cell.label.backgroundColor == [UIColor blackColor]) {
        for (NSDictionary *temp in self.array) {
            if (cell.label.text.length == 1) {
                str = [NSString stringWithFormat:@"0%@", cell.label.text];
            } else {
                str = cell.label.text;
            }
            if ([str isEqualToString:temp[@"day"]] && [monthStr isEqualToString:temp[@"month"]]) {
                [self.tableViewArr addObject:temp];
            }
        }
        
    } else {
        self.tableViewArr = nil;
    }
   [self.tableView reloadData];
}

- (void)popBack:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}



@end
